// NFC_08 [OpenGL Colour Cube Applet].nova // Using namespace declarations. using Library.Math; using Library.NFC; using Library.OpenGL; // The applet class. class ColourCubeApplet : Applet, IRun { // Applet data members. private bool active; private int mouseX, mouseY; private bool mouseButtonDown; private double spinX, spinY; private Matrix4d matrix; private uint cubeDisplayList; // The window's OpenGL rendering context. private RenderingContext renderingContext; // Application class's "main" function. public static void main( String[] args ) { // Create a new applet window with a new applet instance. AppletWindow window = new AppletWindow( new ColourCubeApplet( ), "NFC_08 [OpenGL Colour Cube Applet]", 640, 480 ); // Show the applet window. window.show( true ); // Process the applet window's events. window.processEvents( ); } // Initialize the applet. public virtual void onInit( ) { // Initialize the applet's data members. mouseX = mouseY = 0; spinX = spinY = 0; active = true; mouseButtonDown = false; matrix = new Matrix4d( ); matrix.rotateX( 30.0 ); matrix.rotateY( -45.0 ); // Create an OpenGL rendering context. renderingContext = OpenGL.createContext( graphics ); // Make the new rendering context the current one. OpenGL.makeCurrent( graphics, renderingContext ); // Initialize the context. OpenGL.glClearColor( 0.5f, 0.5f, 0.5f, 0.0f ); OpenGL.glShadeModel( OpenGL.GL_SMOOTH ); OpenGL.glClearDepth( 1.0 ); OpenGL.glEnable( OpenGL.GL_DEPTH_TEST ); // Generate a display list to draw a colour cube. cubeDisplayList = OpenGL.glGenLists( 1 ); OpenGL.glNewList( cubeDisplayList, OpenGL.GL_COMPILE ); ColourCube.drawColourCube( ); OpenGL.glEndList( ); // Create a new thread to update the applet. Thread updateThread = new Thread( this, null ); // Start the thread. updateThread.start( ); } // Run the applet (the update thread's method). public virtual Object run( Object arg ) { // Run while the active flag is set. while ( active ) { // Check if the mouse button is up. if ( mouseButtonDown == false ) { // Spin the cube. rotateCube( ); } // Repaint the applet. repaint( ); } // Set the current OpenGL context to null. OpenGL.makeCurrent( null, null ); // Delete the OpenGL context. OpenGL.deleteContext( renderingContext ); return null; } // On mouse button down event handler. public virtual void onMouseButtonDown( int button ) { // Check for the left mouse button index. if ( button == 0 ) { // Update the state of the left mouse button. mouseButtonDown = true; // Stop the cube from spinning. spinX = spinY = 0; } } // On mouse button up event handler. public virtual void onMouseButtonUp( int button ) { // Check for the left mouse button index. if ( button == 0 ) { // Update the state of the left mouse button. mouseButtonDown = false; } } // On mouse move event handler. public virtual void onMouseMove( int posX, int posY ) { // Update the state of the left mouse button. mouseButtonDown = getKeyState( KeyReference.VK_LBUTTON ); // Check if the mouse button is pressed. if ( mouseButtonDown ) { spinX = (double)( posY - mouseY ) / 2.0; spinY = (double)( posX - mouseX ) / 2.0; // Orientate the cube. rotateCube( ); } mouseX = posX; mouseY = posY; } // On size event handler. public virtual void onSize( int sizeX, int sizeY ) { // Prevent a divide by zero error. if ( sizeY == 0 ) { // Set the height equal to one. sizeY = 1; } // Reset the current viewport. OpenGL.glViewport( 0, 0, sizeX, sizeY ); // Select the projection matrix. OpenGL.glMatrixMode( OpenGL.GL_PROJECTION ); // Reset the projection matrix. OpenGL.glLoadIdentity( ); // Calculate the aspect ratio of the window. OpenGL.gluPerspective( 45.0, (double)sizeX / (double)sizeY, 0.1, 100.0 ); // Select the modelview matrix. OpenGL.glMatrixMode( OpenGL.GL_MODELVIEW ); // Reset the modelview matrix. OpenGL.glLoadIdentity( ); } // On paint event handler. public virtual void onPaint( ) { // Clear the buffers. OpenGL.glClear( OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT ); // Reset the current matrix. OpenGL.glLoadIdentity( ); // Move into the scene. OpenGL.glTranslatef( 0.0f, 0.0f, -3.0f ); // Rotate the colour cube. OpenGL.glMultMatrix( matrix ); // Call the colour cube display list. OpenGL.glCallList( cubeDisplayList ); // Swap the window's OpenGL buffers. OpenGL.swapBuffers( graphics ); } // On close event handler. public virtual void onClose( ) { // Set the active flag to false to stop the rendering thread. active = false; } // Rotate the cube. private void rotateCube( ) { // Create a local rotation matrix. Matrix4d rotationMatix = new Matrix4d( ); // Rotate the rotation matrix. rotationMatix.rotateX( spinX ); rotationMatix.rotateY( spinY ); // Pre-multiply the cube's matrix with the rotation matrix. matrix = rotationMatix.multiply( matrix ); } } // Class to draw a colour cube using OpenGL. class ColourCube { // Draw an OpenGL colour cube. public static void drawColourCube( ) { // Draw the colour cube. OpenGL.glBegin( OpenGL.GL_QUAD_STRIP ); OpenGL.glColor3f( 1.0f, 0.0f, 1.0f ); // Magenta. OpenGL.glVertex3f( -0.5f, 0.5f, 0.5f ); // Near, upper left. OpenGL.glColor3f( 1.0f, 0.0f, 0.0f ); // Red. OpenGL.glVertex3f( -0.5f, -0.5f, 0.5f ); // Near, lower left. OpenGL.glColor3f( 1.0f, 1.0f, 1.0f ); // White. OpenGL.glVertex3f( 0.5f, 0.5f, 0.5f ); // Near, upper right. OpenGL.glColor3f( 1.0f, 1.0f, 0.0f ); // Yellow. OpenGL.glVertex3f( 0.5f, -0.5f, 0.5f ); // Near, lower right. OpenGL.glColor3f( 0.0f, 1.0f, 1.0f ); // Cyan. OpenGL.glVertex3f( 0.5f, 0.5f, -0.5f ); // Far, upper right. OpenGL.glColor3f( 0.0f, 1.0f, 0.0f ); // Green. OpenGL.glVertex3f( 0.5f, -0.5f, -0.5f ); // Far, lower right. OpenGL.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue. OpenGL.glVertex3f( -0.5f, 0.5f, -0.5f ); // Far, upper left. OpenGL.glColor3f( 0.0f, 0.0f, 0.0f ); // Black. OpenGL.glVertex3f( -0.5f, -0.5f, -0.5f ); // Far, lower left. OpenGL.glColor3f( 1.0f, 0.0f, 1.0f ); // Magenta. OpenGL.glVertex3f( -0.5f, 0.5f, 0.5f ); // Near, upper left. OpenGL.glColor3f( 1.0f, 0.0f, 0.0f ); // Red. OpenGL.glVertex3f( -0.5f, -0.5f, 0.5f ); // Near, lower left. OpenGL.glEnd( ); OpenGL.glBegin( OpenGL.GL_QUADS ); OpenGL.glColor3f( 1.0f, 0.0f, 1.0f ); // Magenta. OpenGL.glVertex3f( -0.5f, 0.5f, 0.5f ); // Near, upper left. OpenGL.glColor3f( 1.0f, 1.0f, 1.0f ); // White. OpenGL.glVertex3f( 0.5f, 0.5f, 0.5f ); // Near, upper right. OpenGL.glColor3f( 0.0f, 1.0f, 1.0f ); // Cyan. OpenGL.glVertex3f( 0.5f, 0.5f, -0.5f ); // Far, upper right. OpenGL.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue. OpenGL.glVertex3f( -0.5f, 0.5f, -0.5f ); // Far, upper left. OpenGL.glEnd( ); OpenGL.glBegin( OpenGL.GL_QUADS ); OpenGL.glColor3f( 1.0f, 0.0f, 0.0f ); // Red. OpenGL.glVertex3f( -0.5f, -0.5f, 0.5f ); // Near, lower left. OpenGL.glColor3f( 1.0f, 1.0f, 0.0f ); // Yellow. OpenGL.glVertex3f( 0.5f, -0.5f, 0.5f ); // Near, lower right. OpenGL.glColor3f( 0.0f, 1.0f, 0.0f ); // Green. OpenGL.glVertex3f( 0.5f, -0.5f, -0.5f ); // Far, lower right. OpenGL.glColor3f( 0.0f, 0.0f, 0.0f ); // Black. OpenGL.glVertex3f( -0.5f, -0.5f, -0.5f ); // Far, lower left. OpenGL.glEnd( ); } }